package com.crh.service.impl;

import com.crh.bean.Users;
import com.crh.mapper.UserMapper;
import com.crh.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author Chrui
 * @date 2018/10/07__0:00
 */
@Service
public class UserServiceImpl implements UserService {


    private SqlSession sqlSession = null;
    @Resource
    private UserMapper userMapper ;

    /**
     * 得到用户列表
     *
     * @param rowoffset 偏移量
     * @param limit     每页数量
     * @return
     */
    @Override
    public PageInfo<Users> queryAllUserList(Map<String, Object> userData, Integer rowoffset, Integer limit)throws Exception {
        /*sqlSession = MybatisUtil.getSqlSession();
        userMapper = sqlSession.getMapper(UserMapper.class);*/
        //页码
        int pageNum = rowoffset / limit + 1;
        PageHelper.startPage(pageNum, limit);
        List<Users> usersList = userMapper.queryAllUserList(userData);
        PageInfo<Users> usersPageInfo = new PageInfo<>(usersList);
       /* sqlSession.commit();
        sqlSession.close();*/
        return usersPageInfo;
    }




    /**
     * 用户的登录
     *
     * @param users
     * @return
     */
    @Override
    public Users login(Users users) {
        Users login = userMapper.userLogin(users);
        return login;
    }

    /**
     * 根据用户id更改状态
     *
     * @param userId
     * @param enables
     * @return
     */
    @Override
    public boolean toggleEnable(int userId, int enables) {
        boolean b = userMapper.toggleEnable(userId, enables);
        return b;
    }


}
